﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using Engine.Plot;
using log4net;

namespace Engine
{
    /// <summary>
    /// analize data and find average curve
    /// </summary>
    public class DataAnalizator
    {
        /// <summary>
        /// logger
        /// </summary>
        private static readonly ILog logger = LogManager.GetLogger(typeof(DataAnalizator));
        /// <summary>
        /// analized data
        /// </summary>
        private readonly IDictionary<int, IList<Plot.PlotPoint>> _data;
        
        /// <summary>
        /// standart constructor
        /// </summary>
        /// <param name="data">analized data</param>
        public DataAnalizator(IDictionary<int, IList<Plot.PlotPoint>> data)
        {
            Contract.Requires(data != null,"data must not be null");
            Contract.Requires(data.Count > 0,"data must contain elements");
            _data = data;
        }
        
        /// <summary>
        /// find average values for our data
        /// </summary>
        /// <returns></returns>
        public IList<Plot.PlotPoint> Analize() 
        {
            logger.Info("начинаем анализировать данные");
            //we can start processing
            IList<int> keys = new List<int>(_data.Keys);
            //all point on recalculate in which percolation point is displaced to zero 
            IList<Plot.PlotPoint> allNormValues = new List<PlotPoint>();
            foreach(int k in keys)
            {
                IList<PlotPoint> series = _data[k];
                //need to sort in asceding order
                series = series.OrderBy( point => point.X ).ToList();
                var seriesPercolationThreashold = series.First().X;
                foreach(var point in series)
                {
                    Plot.PlotPoint normPoint = new Plot.PlotPoint(point.X - seriesPercolationThreashold, point.Conductance);
                    allNormValues.Add(normPoint);
                }
            }            
            return allNormValues;
        }

    }
}
